package org.aeon.client.view.components.button;

import org.aeon.client.context.GlobalContext;
import org.aeon.client.view.ApplicationUI;
import org.aeon.utils.ImageSetUtil;
import org.aeon.utils.SwingUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.Arrays;
import java.util.function.Consumer;

/**
 * @author: MissingAeon
 * @date: 2024/2/28 12:44
 * @description: 下拉按钮
 */
public class DropdownButtons extends JPanel {
    private final JButton[] buttons;
    private final Font font = new Font("黑体", Font.PLAIN, 14);
    private JPanel dropdownPanel;

    public DropdownButtons(Color color, String... options) {
        buttons = new JButton[options.length];
        this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        if (color == null) {
            this.setOpaque(false);
        } else this.setBackground(color);

        this.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.lightGray));

        // 创建垂直分隔线
        JSeparator separator = new JSeparator(SwingConstants.VERTICAL);
        // 设置分隔线的最大尺寸
        separator.setMaximumSize(new Dimension(1, Integer.MAX_VALUE));
        separator.setForeground(Color.lightGray);

        final JButton first = new JButton(options[0]);
        first.setFont(font);
        SwingUtil.removeStyle(first);
        buttons[0] = first;

        final Dimension dimension = new Dimension(70, 28);
        first.setPreferredSize(dimension);


        final JButton dropdown = new JButton(new ImageIcon(ImageSetUtil.getImage("dropdown", 15, 15)));
        SwingUtil.removeStyle(dropdown);

        dropdown.setPreferredSize(new Dimension(20, 28));


        this.add(first);
        this.add(separator);
        this.add(dropdown);

        dropdown.addActionListener(e -> {
            if (dropdownPanel == null) createDropdownBox();
            displayList();
        });

        for (int i = 0; i < options.length; i++) {
            if (i != 0) {
                final JButton button = new JButton(options[i]);
                SwingUtil.removeStyle(button);
                button.setPreferredSize(new Dimension(82, dimension.height));
                button.setFont(font);
                buttons[i] = button;
            }
            int finalI = i;
            buttons[i].addActionListener(e -> clickAfterHandler(finalI));
            buttons[i].addActionListener(e -> hideList());
        }
    }


    private void createDropdownBox() {
        dropdownPanel = new JPanel();
        dropdownPanel.setLayout(new BoxLayout(dropdownPanel, BoxLayout.Y_AXIS));
        dropdownPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.lightGray));

        Arrays.stream(buttons).skip(1).forEach(button -> {
            dropdownPanel.add(button);
            // 水平
            JSeparator sep = new JSeparator(SwingConstants.HORIZONTAL);
            // 设置分隔线的最大尺寸
            sep.setMaximumSize(new Dimension(Integer.MAX_VALUE, 1));
            sep.setForeground(Color.lightGray);
            dropdownPanel.add(sep);
        });

        final ApplicationUI mainUI = GlobalContext.getContext().getMainUI();
        dropdownPanel.setBounds(this.getLocationOnScreen().x - mainUI.getLocationOnScreen().x - mainUI.getInsets().left,
                this.getLocationOnScreen().y - mainUI.getLocationOnScreen().y + this.getHeight() - mainUI.getInsets().top - 1,
                100, (buttons.length - 1) * 28);

        dropdownPanel.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                hideList();
            }
        });
    }

    /**
     * 给每一个按钮添加一个事件
     * 获得按钮的序号
     *
     * @param consumer 事件内容
     */
    public void addActionListener(Consumer<Integer> consumer) {
        for (int i = 0; i < buttons.length; i++) {
            int finalI = i + 1;
            buttons[i].addActionListener(e -> consumer.accept(finalI));
        }
    }

    /**
     * 点击按钮后操作
     *
     * @param number
     */
    public void clickAfterHandler(int number) {
        final JLabel label = new JLabel("已" + buttons[number].getText());

        this.removeAll();

        label.setAlignmentX(Component.CENTER_ALIGNMENT);
        label.setFont(font);
        label.setForeground(Color.WHITE);
        this.add(Box.createHorizontalGlue());
        this.add(label);
        this.add(Box.createHorizontalGlue());
        this.setOpaque(false);
        this.setBorder(null);
        this.repaint();
    }

    /**
     * 展示列表
     */
    private void displayList() {
        if (!dropdownPanel.isShowing()) {
            GlobalContext.getContext().getMainUI().addPopUpLayer(dropdownPanel);
        }
        dropdownPanel.requestFocus();
    }

    /**
     * 隐藏列表
     */
    private void hideList() {
        if (dropdownPanel != null && dropdownPanel.isShowing()) {
            GlobalContext.getContext().getMainUI().removePopUpLayer(dropdownPanel);
        }
    }
}
